package com.gxa.www.cl.service.impl;

import com.gxa.www.cl.mapper.DrugMapper;
import com.gxa.www.cl.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
public class DrugServiceImpl implements DrugService {

    private static final Logger logger = LoggerFactory.getLogger(DrugServiceImpl.class);

    @Autowired
    private DrugMapper drugMapper;

    @Override
    public List<Map<String,Object>> getDrugs() {
        return drugMapper.getDrugs();
    }

    @Override
    public void deleteDrug(Integer id) { // 修改: 将 Long 类型改为 Integer 类型
        drugMapper.deleteDrug(id);
    }

//    @Override
//    @Transactional // 确保方法在事务中执行
//    public int addDrug(Map<String, Object> formData) {
//        Drug drug = new Drug();
//        drug.setName((String) formData.get("name"));
//        drug.setLianxiren((String) formData.get("lianxiren"));
//        drug.setDianhua((String) formData.get("dianhua"));
//        drug.setYoubian((String) formData.get("youbian"));
//        drug.setBeizhu((String) formData.get("beizhu"));
//        drug.setStatus(formData.get("status") != null ? (Integer) formData.get("status") : 1);
//        drug.setSupplierName((String) formData.get("supplierName"));
//        drug.setUnit((String) formData.get("unit"));
//        drug.setExpiryDate((String) formData.get("expiryDate"));
//        drug.setRemark((String) formData.get("remark"));
//
//        // 检查数据库中是否已经存在相同的数据
//        Drug existingDrug = drugMapper.selectDrugByFields(drug);
//        if (existingDrug != null) {
//            logger.warn("Duplicate drug data detected: {}", drug);
//            return 0; // 返回0表示未插入数据
//        }
//
//        try {
//            int result = drugMapper.insertDrug(drug);
//            if (result > 0) {
//                return result;
//            } else {
//                throw new RuntimeException("插入药品失败");
//            }
//        } catch (Exception e) {
//            logger.error("Error adding drug: {}", drug, e);
//            throw e;
//        }
//    }
}